﻿/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_FILE_H
#define MK_FILE_H
#include "MkUtil/MkTypes.h"

#ifdef _WIN32
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
# define __dirfd(dp)    ((dp)->dd_fd)

struct dirent
{
    long            d_ino;              /* inode number*/
    off_t           d_off;             /* offset to this dirent*/
    unsigned short  d_reclen; /* length of this d_name*/
    Uint8   d_type;    /* the type of d_name*/
    char            d_name[1];          /* file name (null-terminated)*/
};
typedef struct _dirdesc
{
    int             dd_fd;      /** file descriptor associated with directory */
    long            dd_loc;     /** offset in current buffer */
    long            dd_size;    /** amount of data returned by getdirentries */
    char            *dd_buf;    /** data buffer */
    int             dd_len;     /** size of data buffer */
    long            dd_seek;    /** magic cookie returned by getdirentries */
    HANDLE          handle;
    struct dirent   *index;
} DIR;


int mkdir(const char *path, int mode);
DIR *opendir(const char *);
int closedir(DIR *);
struct dirent *readdir(DIR *);

#endif // defined(_WIN32)

class MkApi CMkFile
{
public:
    static Uint32 GetLogPath(MkString &strPath);
    static Uint32 GetConfigPath(MkString &strPath);
    static Uint32 GetDownloadPath(MkString &strPath);
    static Uint32 GetFontFilePath(MkString &strPath);
    static void DeleteLocalFile(const MkString &strFileName);
    static BOOL CheckDirIsExist(const MkString &strPath);
    static BOOL CheckFileIsExist(const MkString &strFileName);
    static BOOL IsDir(const MkString &strFilePath);
    static Uint32 MakeDir(const MkString &strPath);
    static void RemoveDir(const MkString& strPath);
    static Uint32 BackUpFile(const MkString &strFrom, const MkString &strTo);
    static Uint32 GetFileSize(const MkString &strFileName);
    static Uint32 ReadFileBuffer(const MkString &FileName, MkString &Content);
    static Uint32 WriteFileBuffer(const MkString &FileName, const MkString &FileBuffer);
    static Uint32 WriteBase64FileBuffer(const MkString &FileName, const MkString &Base64Buffer);
    static Uint32 CreateBasePath(const MkString &strPath);
    static Uint32 GetModuleFilePath(MkString &strPath);
    static Uint32 ScanDir(const MkString &strPath, MkVectorString &vecChild);
protected:
private:
};

#endif
